﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using System.Collections.ObjectModel;

namespace SmartSpace.UI.Silverlight.Dynamic.Controls
{
    public partial class SelectOrgWindow : ChildWindow
    {
        #region Variables

        private ObservableCollection<Org> m_orgs;
        private List<Org> m_selectedOrgs = new List<Org>();

        private StringBuilder m_orgIdBuilder = new StringBuilder();
        private StringBuilder m_orgNameBuilder = new StringBuilder();

        private List<Org> m_initOrgs;

        public List<Org> SelectedOrgs
        {
            get
            {
                return m_selectedOrgs;
            }
        }

        #endregion

        private bool m_isSubOnly = false;
        private string m_userName;

        #region Constructors

        public SelectOrgWindow(bool subOnly, string userName, List<Org> initOrgs)
        {
            InitializeComponent();
            m_isSubOnly = subOnly;
            m_userName = userName;
            m_initOrgs = initOrgs;

            this.Loaded += new RoutedEventHandler(SelectOrgWindow_Loaded);
        }

        void SelectOrgWindow_Loaded(object sender, RoutedEventArgs e)
        {
            m_busy.IsBusy = true;
            DynamicWebServiceSoapClient client = DynamicModule.CreateProxy();
            client.GetOrgsCompleted += new EventHandler<GetOrgsCompletedEventArgs>(client_GetOrgsCompleted);
            client.GetOrgsAsync();
        }

        void client_GetOrgsCompleted(object sender, GetOrgsCompletedEventArgs e)
        {
            if (e.Error == null && e.Result.IsSuccess)
            {
                m_orgs = e.Result.Data;
                if (!m_isSubOnly)
                {
                    foreach (Org rootOrg in m_orgs)
                    {
                        foreach (Org childOrg in rootOrg.Children)
                        {
                            childOrg.Children = new ObservableCollection<Org>();
                        }
                    }
                }
                
                m_treeViewOrganizations.ItemsSource = m_orgs;

                SeletetInitOrgs();

            }
            else
            {
                MessageBox.Show("获取失败!");
            }
            m_busy.IsBusy = false;
        }

        #endregion

        public void InitOrgs(List<Org> orgs)
        {
            m_initOrgs = orgs;
            SeletetInitOrgs();
        }

        private void SeletetInitOrgs()
        {
            if (m_initOrgs == null)
            {
                return;
            }

            if (m_orgs == null)
            {
                return;
            }


            Stack<Org> stackOrg = new Stack<Org>();
            foreach (Org Org in m_orgs)
            {
                stackOrg.Push(Org);
            }

            while (stackOrg.Count > 0)
            {
                Org org = stackOrg.Pop();
                foreach (Org initOrg in m_initOrgs)
                {
                    if (initOrg.Id == org.Id)
                    {
                        org.IsSelected = true;
                        break;
                    }
                }

                foreach (Org childOrg in org.Children)
                {
                    stackOrg.Push(childOrg);
                }
            }
        }

        private void m_buttonOK_Click(object sender, RoutedEventArgs e)
        {
            Stack<Org> stackOrg = new Stack<Org>();
            foreach (Org Org in m_orgs)
            {
                stackOrg.Push(Org);
            }

            //新增逻辑 只有末级子节点才被设置为选中单位
            while (stackOrg.Count > 0)
            {
                Org org = stackOrg.Pop();
                if (org.IsSelected && (org.Children== null || org.Children.Count == 0))
                {
                    m_selectedOrgs.Add(org);
                }

                foreach (Org childOrg in org.Children)
                {
                    if(childOrg.IsSelected && (childOrg.Children== null || childOrg.Children.Count == 0))
                    {
                        stackOrg.Push(childOrg);
                    }
                }
            }

            DialogResult = true;
        }

        private void m_buttonCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
        }

        private void m_buttonClear_Click(object sender, RoutedEventArgs e)
        {
            Stack<Org> stackOrg = new Stack<Org>();
            foreach (Org Org in m_orgs)
            {
                stackOrg.Push(Org);
            }

            while (stackOrg.Count > 0)
            {
                Org org = stackOrg.Pop();
                org.IsSelected = false;

                foreach (Org childOrg in org.Children)
                {
                    stackOrg.Push(childOrg);
                }
            }
        }
    }
}

